import time

from common.base import Base, open_browser
from page.login_page import LoginPage, login_url


class AddressPage(Base):
    """创建定位器"""
    user_center_loc = ("link text", "用户中心")  # 用户中心
    shipping_address_loc = ("css selector", "a[href *='address']")  # 收货地址
    province_loc = ("id", "selProvinces_0")  # 省
    city_loc = ("id", "selCities_0")  # 市
    district_loc = ("id", "selDistricts_0")  # 区
    name_loc = ("name", "consignee")  # 收货人姓名
    detailed_address_loc = ("name", "address")  # 详细地址
    email_loc = ("name", "email")  # 电子邮件地址
    postal_code_loc = ("name", "zipcode")  # 邮政编码
    tel_loc = ("id", "tel_0")  # 电话
    mobile_loc = ("name", "mobile")  # 手机
    submit_loc = ("name", "submit")  # 新增收货地址
    delete_loc = ("name", "button")  # 删除

    """创建操作"""

    def click_user_center(self):
        """点击用户中心"""
        self.click(self.user_center_loc)

    def click_shipping_address(self):
        """点击收货地址"""
        self.click(self.shipping_address_loc)

    def choice_provinces(self):
        """随机选取省、市、区"""
        self.random_choice_select(self.province_loc)  # 省
        time.sleep(2)
        self.random_choice_select(self.city_loc)  # 市
        time.sleep(2)
        self.random_choice_select(self.district_loc)  # 区
        time.sleep(2)

    def input_name(self, name):
        """输入收货人姓名"""
        self.send_keys(self.name_loc, name)
        time.sleep(1)

    def input_detailed_address(self, address):
        """输入收货地址"""
        self.send_keys(self.detailed_address_loc, address)
        time.sleep(1)

    def input_email(self, email):
        """输入电子邮件地址"""
        self.send_keys(self.email_loc, email)
        time.sleep(1)

    def input_postal_code(self, code):
        """输入邮政编码"""
        self.send_keys(self.postal_code_loc, code)
        time.sleep(1)

    def input_tel(self,tel):
        """输入电话"""
        self.send_keys(self.tel_loc,tel)
        time.sleep(1)

    def input_mobile(self,mobile):
        """输入手机"""
        self.send_keys(self.mobile_loc,mobile)
        time.sleep(1)

    def click_submit_add(self):
        """点击新增收货地址"""
        self.click(self.submit_loc)
        time.sleep(2)

    def click_delete(self):
        """点击删除并点击弹框确定"""
        self.click(self.delete_loc)
        time.sleep(2)
        self.accept()
        time.sleep(2)

if __name__ == '__main__':

    driver = open_browser()
    address = AddressPage(driver)
    login = LoginPage(driver)
    login.open_url(login_url)
    login.input_username("yangjun")
    login.input_password("123456")
    login.click_submit()
    address.click_user_center()
    address.click_shipping_address()